#include "autoresponder.h"
#include "dialog_topo_setting.h"
#include "ui_dialog_topo_setting.h"

#include <QStandardItemModel>
#include <QStyleFactory>

DialogTopoSetting::DialogTopoSetting(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::DialogTopoSetting)
{
    ui->setupUi(this);


    ui->treeView->setStyle(QStyleFactory::create("windows"));


    QStandardItemModel *model = new QStandardItemModel(ui->treeView);

    ui->treeView->setModel(model);

    update_tree();


}

DialogTopoSetting::~DialogTopoSetting()
{
    delete ui;
}

void DialogTopoSetting::update_tree()
{
    QStandardItemModel *model = (QStandardItemModel *) ui->treeView->model();
    model->clear();

    QStringList headerLabels ;
    headerLabels <<   "Node" << "Phy" << "target node" << "target phy";
    model->setHorizontalHeaderLabels(headerLabels);

    QList<int> node_ids = AutoResponder::Instance()->test_node.keys();


    for ( int i = 0 ; i < node_ids.count(); i ++)
    {
        TOPO_NODE * node = AutoResponder::Instance()->test_node[node_ids[i]];

        QStandardItem * item = new QStandardItem(QString::number(node->_id));
        //  model->setData(model->index(i,0), node->_id);
        model->appendRow( item);

        foreach(TOPO_PHY * phy, node->phy)
        {
            if (phy != NULL)
        {
            QList<QStandardItem*> items_1;
            items_1 << new QStandardItem("");
            items_1<<new QStandardItem(QString::number(phy->_phy));

            if ( phy->target)
            {
                items_1<< new QStandardItem(QString::number(phy->target->_node ));

                items_1<<new QStandardItem(QString::number(phy->target->_phy ));
            }
            item->appendRow(items_1);
            }
        }

    }

    ui->treeView->expandAll();

}

void DialogTopoSetting::on_pushButton_remove_phy_clicked()
{
    QStandardItemModel *model = (QStandardItemModel *) ui->treeView->model();
    QModelIndex index = ui->treeView->currentIndex();

   int node_id = index.parent().data().toInt();
    int phy_id = model->index(index.row(), 1, index.parent()).data().toInt();

   AutoResponder::Instance()->remove_phy(node_id,phy_id);

    update_tree();
}


void DialogTopoSetting::on_pushButton_disconnect_clicked()
{
    QStandardItemModel *model = (QStandardItemModel *) ui->treeView->model();
    QModelIndex index = ui->treeView->currentIndex();

    int node_id = index.parent().data().toInt();
    int phy_id = model->index(index.row(), 1, index.parent()).data().toInt();

    AutoResponder::Instance()->remove_phy_target( node_id,phy_id);

    update_tree();
}


void DialogTopoSetting::on_pushButton_remove_node_clicked()
{
    QStandardItemModel *model = (QStandardItemModel *) ui->treeView->model();
    QModelIndex index = ui->treeView->currentIndex();

    int node_id = model->index(index.row(), 0, index.parent()).data().toInt();
    if (node_id==0)
        node_id = index.parent().data().toInt();

    AutoResponder::Instance()->remove_node(node_id);

    update_tree();
}

